<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<meta name="author" content="hexinyang">
		<title>解灰</title>
		<script language="javascript" src="../script/common.js" type="text/javascript"></script>
		<script language="javascript" src="../script/card.js" type="text/javascript"></script>
		<script language="javascript" type="text/javascript">
	var pd = {
		pin : null,
		asn : null,
		grayTradeId : null,
		accCode : null
	}
	function load(){
		Eui.showNavigation();
		setPages(["readcardview","detview"]);
		Rpc.loadPage("formDetail","card_detail_div.html");	
	}
	
	function onReadCard(){
		try{
			var accList = []
			getCard();
			pd.asn = getAsn();
			pd.pin = CardMgnt.inputCardPin()
			var fundGray = getLockProof(pd.pin);
			if (fundGray) accList.push(AccountType.FUND)
			var pointGray = getLockLoyatyProof(pd.pin);
			if (pointGray) accList.push(AccountType.POINT)
			
			if (accList.length<1) throw new Error("非灰锁状态卡片，不能解灰！")
			Common.initDict(grayform.accountType,Common.getSubsetItems("account_type",accList));
			var info = ""
			for (var i=0;i<accList.length;i++){
				var ac = accList[i];
				if (info) info += "、";
				info += Common.getText("account_type",ac); 
			}
			showInformation("卡片的"+info + "钱包处于灰锁状态，需要解灰！");
			
			pd.accCode = accList[0];
			var grayCard;
			if (pd.accCode == AccountType.FUND){
				grayCard = getLockProofData(pd.pin);
				$("grayunit").innerHTML = "元"
			}else{
				grayCard = getLockLoyaltyProofData(pd.pin)
				$("grayunit").innerHTML = "分"
			}
			var data = DWR.syncCall("grayTradeService.findGrayTrade",pd.asn,grayCard.offOnlineNo,pd.accCode);

			var card = data.card;
			if (!CardMgnt.checkInUseCard(card)) return;
			
			var cardObj = CardMgnt.convertCardObject(data);
			Common.setValues("detailForm",cardObj);
			var grayTrade = data.grayTrade;
			pd.grayTradeId = grayTrade.id
			grayTrade.acquirerName = data.acquirerName;
			grayTrade.amount = Format.fenToYuan(grayTrade.amount);
			Common.setValues("grayform",grayTrade);
			$("ungraybtn").disabled = false;
			$("readCardBtn").disabled = true;		
			
		}catch(e){showError(e.description);onCancel();}
	}
	function unlockGrayCard(){			 //解灰		
		if (!CardMgnt.checkCardAsn(pd.asn)) return;
		if (!showQuestion("是否解灰?")) return;
		
		try{
			var result;
			var money = Format.yuanToFen($("grayform").amount.value);
			getCard();
			if (pd.accCode == AccountType.FUND){
				var data = initGreyUnlock(pd.pin);
				result = DWR.syncCall("grayTradeService.unlockGrayCard",data,pd.asn,money,pd.grayTradeId);
				var mac3 = greyUnlock(money,result.macData);
				data.macData = mac3; //注意更改data.mac
				//DWR.syncCall('pbocService.verifyGreyUnlock',data,pd.asn,money,result.macData.serverTime)
			}else{
				var data = initGreyLoyatyUnlock(pd.pin);
				result = DWR.syncCall("grayTradeService.unlockGrayCard",data,pd.asn,money,pd.grayTradeId);
				var mac3 = greyLoyaltyUnlock(money,result.macData);
				data.macData = mac3; //注意更改macData.mac
				//DWR.syncCall('pbocService.verifyGreyLoyaltyUnlock',data,pd.asn,money,result.serverTime)
			}
			showInformation(Common.getText("account_type",pd.accCode) + "钱包解灰成功!");	
			switchPage("detview");
			CardMgnt.showPrintView("detview","ungray_bill",result.logId,"解灰凭证");
			onCancel();
		}catch(e){
			showError(e.description);
			return;
		}
	}

	function onCancel(){
		pd = {}
		$("detailForm").reset();
		$("grayform").reset();
		$("ungraybtn").disabled = true;
		$("readCardBtn").disabled = false;
		switchPage("readcardview");
	}
		</script>
	</head>
	<body onload="load()">
	<div class="nav"><div id="nav">&nbsp;</div></div>
		
	<div id="readcardview">
		<div id="carddiv">
			<form id="detailForm" name="detailForm" action="">
				<div class="form">
					<div class="formTitle">卡片明细信息</div>
					<div id="formDetail"></div>
				</div>
			</form>
		</div>
		<div class="form">
			<div class="formTitle">灰交易信息</div>
			<form id="grayform" name="grayform" action="">
				<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="col2 label5">
					<tr>
						<td><label>置灰站点</label> <input name="acquirerName" type="text" readonly class="readonly"> <span class="des">*</span></td>
						<td><label>置灰日期</label> <input name="occurTime" type="text" readonly class="readonly"> <span class="des">*</span></td>
					</tr>
					<tr>
						<td><label>钱包类型</label> <select name="accountType" disabled class="disabled"></select> <span class="des">*</span></td>
						<td><label>灰交易额</label> <input name="amount" type="text" readonly class="readonly"> <span id="grayunit">元</span><span class="des">*</span></td>
					</tr>
				</table>
			</form>
		</div>
		
		<p>
			<button id="readCardBtn" onclick="onReadCard()">读 卡</button>
			<button id="ungraybtn" onclick="unlockGrayCard()" disabled>解 灰</button>
			<button onclick="onCancel()">重 置</button>
		</p>
	</div>
	<div id="detview" style="display:none;"></div>
		
	</body>
</html>